package com.codingday.app;

import java.util.ArrayList;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.QuickContactBadge;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.CheckedTextView;

public class TODOListAdapter extends ArrayAdapter<TODO>{
		
		private Context mContext;
		ArrayList<TODO> todolist;
		LayoutInflater mInflater;
		int mLayout;
		View mView;
		// ARGB
		private int[] colors = new int[] { 0xFF4579b5 };	
		private int[] colorLabels = new int[] {0xFFd5e6d5, 0xFF74c0db};
		private int[] textColors = new int[] {0xFFe8e8e8};
		private int[] completedTextColors = new int[] {  0xFF00458a};
		
		
		// QuickAction
		ActionItem chart;// = new ActionItem();
		ActionItem production;// = new ActionItem();

		public TODOListAdapter(Context context, int textViewResourceId, ArrayList<TODO> todolist) {
			super(context, textViewResourceId, todolist);
			this.mContext = context;
			this.todolist = todolist;
			Log.i("TODOAdapter","Constructor : " + todolist.size());
			// TODO Auto-generated constructor stub
			this.mLayout = textViewResourceId;
			this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		
		}

		

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			//return super.getCount();
			return todolist.size();
		}



		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			//return super.getItemId(position);
			return position;
		}



		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			mView = convertView;
			final TODOViewHolder todoViewHolder;
			
			if(mView == null){
				//mView = mInflater.inflate(R.layout.row_todo, null);
				mView = mInflater.inflate(mLayout, parent, false);
			
				todoViewHolder = new TODOViewHolder();
				todoViewHolder.colorLabel = (TextView) mView.findViewById(R.id.todo_color_label);
				todoViewHolder.titleText = (TextView) mView.findViewById(R.id.todotitle);
				//todoViewHolder.localText = (TextView) mView.findViewById(R.id.todo_location_title);
				//todoViewHolder.enableToggleButton = (ToggleButton) mView.findViewById(R.id.todo_toggle_enable_button);
				todoViewHolder.enableCheckBox = (CheckBox) mView.findViewById(R.id.todo_check_enable_button);
				
				mView.setTag(todoViewHolder);
			}else{
				todoViewHolder = (TODOViewHolder) mView.getTag();
			}
			
			//mView = vi.inflate(R.layout.row_todo, null);
			final TODO todo = todolist.get(position);
			
			if(todo != null){
				//Log.i("getView",""+todo.getTitle());
				//TextView ttv = (TextView) mView.findViewById(R.id.todotitle);
				//TextView ltv = (TextView) mView.findViewById(R.id.todo_location_title);
				//if(ttv != null && ltv != null){
					final int colorIndex = position % colors.length;
					final int textColorIndex = position % textColors.length;
					final int colorLabelIndex = position % colorLabels.length;
					//final boolean isEnabled = todo.isEnabled();
					//final boolean isCompleted = todo.isCompleted();
					final boolean completed = todo.isCompleted();
					mView.setBackgroundColor(colors[colorIndex]);
					todoViewHolder.colorLabel.setBackgroundColor(colorLabels[colorLabelIndex]);
					
					//Log.i("Title Length - ","Position: "+position +", Length:"+ todo.getTitle().length());
					//ttv.setText(todo.getTitle());
					//ltv.setText("home");

					
					todoViewHolder.titleText.setText(todo.getTitle());
					if(completed){
						todoViewHolder.titleText.setTextColor(completedTextColors[textColorIndex]);
					}else{
						todoViewHolder.titleText.setTextColor(textColors[textColorIndex]);
					}
					//todoViewHolder.localText.setText("Home - "+position);
					//todoViewHolder.localText.setText("[ #"+todo.getID()+" ]");
					//todoViewHolder.enableToggleButton.setChecked(todo.isEnabled());

					
					
					final int pos = position;

					//todoViewHolder.titleText.setClickable(true);
					
					todoViewHolder.titleText.setOnClickListener(new OnClickListener(){

						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							//Toast.makeText(mContext, "touch : "+pos, 1).show();
							final long ID = todo.getID();
							
							// Temporal
							//goToViewToDoActivity(ID);
							
							final QuickAction qa = new QuickAction(v);
							
							
							
							ActionItem done = new ActionItem();
							done.setTitle("Done");
							done.setIcon(v.getResources().getDrawable(R.drawable.ic_menu_mark));

							
							
							
							done.setOnClickListener(new OnClickListener(){

								@Override
								public void onClick(View v) {
									todo.setCompleted(true);
									//SaveManager.FinishTODO(todo);
									TODOListView.getInstance().reset();
									qa.dismiss();
								}
								
							});


							ActionItem delete = new ActionItem();
							delete.setTitle("Delete");
							delete.setIcon(v.getResources().getDrawable(R.drawable.ic_menu_delete));
							
							delete.setOnClickListener(new OnClickListener(){

								@Override
								public void onClick(View v) {
									// TODO Auto-generated method stub
									//TODOListManager.deleteTODO(ID);
									DeleteDialog(ID,qa);
									//qa.dismiss();
								}
								
							});
							
							

							ActionItem edit = new ActionItem();
							edit.setTitle("Edit");
							edit.setIcon(v.getResources().getDrawable(R.drawable.ic_menu_edit));
							edit.setOnClickListener(new OnClickListener(){

								@Override
								public void onClick(View arg0) {
									// TODO Auto-generated method stub
									TODOListView.getInstance().gotoEditToDoActivity(ID);
									qa.dismiss();
								}
								
							});


							ActionItem DetailInfo = new ActionItem();
							DetailInfo.setTitle("View");
							DetailInfo.setIcon(v.getResources().getDrawable(R.drawable.ic_menu_view));
	
							
							DetailInfo.setOnClickListener(new OnClickListener(){

								@Override
								public void onClick(View v) {
									// TODO Auto-generated method stub
									//Toast.makeText(v.getContext(), "chart Selected", 1).show();
									qa.dismiss();
									TODOListView.getInstance().goToViewToDoActivity(ID);
								}
								
							});
							
							ActionItem Reuse = new ActionItem();
							Reuse.setTitle("Re-Use");
							Reuse.setIcon(v.getResources().getDrawable(R.drawable.ic_menu_refresh));
	
							
							Reuse.setOnClickListener(new OnClickListener(){

								@Override
								public void onClick(View v) {
									// TODO Auto-generated method stub
									//Toast.makeText(v.getContext(), "chart Selected", 1).show();
									todo.setCompleted(false);
									qa.dismiss();
									TODOListView.getInstance().reset();
									//TODOListView.getInstance().goToViewToDoActivity(ID);
								}
								
							});
							
							if(todo.isCompleted() == false){
								qa.addActionItem(done);
							}
							if(todo.isCompleted() == false){
								qa.addActionItem(edit);
							}
							if(todo.isCompleted == true){
								qa.addActionItem(Reuse);
							}
							qa.addActionItem(DetailInfo);
							qa.addActionItem(delete);
;							qa.setAnimStyle(QuickAction.ANIM_AUTO);
							qa.show();
						}
						
					});
					
					
					//todoViewHolder.enableToggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener(){
					
					/*
					todoViewHolder.enableCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){
						@Override
						public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
							// TODO Auto-generated method stub
							Log.i("TODOListAdapter", "onCheckedChanged");
							if(todo.isCompleted() == false){
							//if(isCompleted){	
								if(isChecked == true){
									Log.i("TODOListAdapter","onCheckedChanged - true");
									//todolist.get(pos).Enable();
									todo.Enable();
								}else{
									Log.i("TODOListAdapter","onCheckedChanged - false");
									//todolist.get(pos).Disable();
									todo.Disable();
								}
							}
						}
						
					});
					*/
					todoViewHolder.enableCheckBox.setOnClickListener(new OnClickListener(){

						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							Log.i("TODOListAdapter","onClickListener");
							if(todoViewHolder.enableCheckBox.isChecked()){
								Log.i("TODOListAdapter","onCheckedChanged - true");
								//todolist.get(pos).Enable();
								todo.Enable();
							}else{
								Log.i("TODOListAdapter","onCheckedChanged - false");
								//todolist.get(pos).Disable();
								todo.Disable();
							}
						}
						
					});
					
					//titleV.setText("hello" + position);
					//titleV.setT
				//}
					if(todo.isEnabled()){
						todoViewHolder.enableCheckBox.setChecked(true);
					}else{
						todoViewHolder.enableCheckBox.setChecked(false);
					}

					if(completed){
						todoViewHolder.titleText.setPaintFlags(todoViewHolder.titleText.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
						todoViewHolder.enableCheckBox.setEnabled(false);
						
					}else{
						todoViewHolder.titleText.setPaintFlags(todoViewHolder.titleText.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
						todoViewHolder.enableCheckBox.setEnabled(true);						
					}
			}
			
			
			return mView;
		}
		


		private void DeleteDialog(final long ID, final QuickAction qa){
			
			AlertDialog.Builder alt_bld = new AlertDialog.Builder(this.getContext());
			
			alt_bld.setMessage("Do you want to delete this TODO ?")
				.setCancelable(false).setPositiveButton("Yes",
						new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							// Action for 'Yes' Button
							//Toast.makeText(, "Delete", 1);
							TODOListManager.deleteTODO(ID);
							TODOListView.getInstance().reset();
							//goBackToList();
							qa.dismiss();
						}
				})
				.setNegativeButton("No",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								// Action for 'NO' Button
								dialog.cancel();
							}
				});
			AlertDialog alert = alt_bld.create();
			
			// Title for AlertDialog
			//alert.setTitle("Title");
			// Icon for AlertDialog
			//alert.setIcon(R.drawable.icon);
			alert.show();
		}
}
